package com.example.sw_1.myapplication;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;

/**
 * 安卓程序主入口
 */
public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      
        test();
    }

    public void test() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //测试            
                System.out.println(executeHttpPost());
            }
        }).start();
    }

    //post
    public String executeHttpPost() {
//        Gson gson = new Gson();
//        List<ShipType> persons = new ArrayList<ShipType>();
//        persons = gson.fromJson(executeHttpPost(), new TypeToken<List<ShipType>>() {
//        }.getType());
//        System.out.println("-------------------------->" + persons);
//        for (int i = 0; i < persons.size(); i++) {
//              System.out.println("i=" + i + "<-------->" + persons.get(i).getShipType());
//        }
        String result = null;
        URL url = null;
        HttpURLConnection connection = null;
        InputStreamReader in = null;
        String data = "LONGY U007";
//       URLEncoder.encode(data, "utf-8");
        try {
			//对参数中的特殊符号进行编译Uri.encode(data)
            url = new URL("****************" + Uri.encode(data));
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Charset", "utf-8");
            DataOutputStream dop = new DataOutputStream(connection.getOutputStream());
            dop.writeBytes("token=alexzhou");
            dop.flush();
            dop.close();
            in = new InputStreamReader(connection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(in);
            StringBuffer strBuffer = new StringBuffer();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                strBuffer.append(line);
            }
            result = strBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("result=" + result);
        return result;
    }

}